home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3522 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.3 KB  |  62 lines

  1. Path: news.NetVision.net.il!news
  2. From: Moti Saadon <moti@netmanage.co.il>
  3. Newsgroups: comp.lang.c++
  4. Subject: [] overloading
  5. Date: Wed, 24 Jan 96 15:33:27 PDT
  6. Organization: NetVision LTD.
  7. Message-ID: <NEWTNews.822526719.31981.moti@motisaad.netmanage.co.il>
  8. NNTP-Posting-Host: motisaan.netmanage.co.il
  9. Mime-Version: 1.0
  10. Content-Type: TEXT/PLAIN; charset=US-ASCII
  11. X-Newsreader: NEWTNews & Chameleon -- TCP/IP for MS Windows from NetManage
  12.  
  13.  
  14. How can help me and tell me why the vc++4.0
  15.  
  16. dosn't compile the definition of the subscript []operator?
  17.  
  18. If the subscript is online the compile succes.
  19.  
  20. -----------------------start code-----------------------------------
  21. //execption.cpp
  22.  
  23. #include <stdio.h>
  24. #include <iostream.h>
  25.  
  26.  
  27.  
  28.  
  29. template <class Type>
  30. class Array {
  31.     Type *array;
  32.     int size;    //size of the array
  33. public:
  34.     Array (int sz)
  35.     { array = new Type[size = sz];}
  36.     ~Array () {delete [] array;}
  37.     Type &operator [] (int i);
  38. /*    {
  39.     cout << "\nsize = " << size  << " i = " << i  << "\n"; 
  40.         if (i<0 || i>=size) 
  41.              throw Range(0, size-1, i);
  42.  
  43.         return array[i];
  44.         }
  45. */
  46. };
  47.  
  48.  
  49.  
  50. //now comes the definition of the subscript operator
  51. //this code can't compile. why?
  52. template <class Type>
  53. Type &Array::operator[] (int i)
  54. {
  55.     if (i<0 || i>=size)
  56.         throw Range(0, size-1, i);
  57.     return array[i];
  58. }
  59.  
  60. -----------------------end code-----------------------------------
  61.  
  62.